package com.eascs.admin.common.config;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.eascs.ScanIndex;
import com.eascs.app.context.ConfigContext;
import com.eascs.app.dao.ConnectionPoolDataSource;
import com.eascs.app.dao.DataSourceSetting;
import com.eascs.app.dao.JdbcSession;
import com.eascs.app.dao.mongo.MongoSession;
import com.eascs.app.dao.mongo.MongoSetting;
import com.eascs.common.dfs.fastdfs.FastDfsClient;
import com.eascs.common.ftp.FtpConfig;
import com.eascs.web.DefaultAppConfig;

@Configuration
@EnableTransactionManagement(proxyTargetClass = true)
@ComponentScan(basePackageClasses = { ScanIndex.class })
public class AppConfig extends DefaultAppConfig {
	
	private static List<String> initSqls = new ArrayList<String>();

	{
		initSqls.add("set names utf8mb4;");
	}
	
	@Bean
	public DataSourceSetting o2oDataSourceSetting() {
		DataSourceSetting dataSourceSetting = new DataSourceSetting();
		ConfigContext.bindConfigObject(dataSourceSetting, "db1.");
		return dataSourceSetting;
	}

	@Bean
	public DataSourceSetting oaDataSourceSetting() {
		DataSourceSetting dataSourceSetting = new DataSourceSetting();
		ConfigContext.bindConfigObject(dataSourceSetting, "db2.");
		return dataSourceSetting;
	}
	
	@Bean
	public DataSourceSetting reportDataSourceSetting() {
		DataSourceSetting dataSourceSetting = new DataSourceSetting();
		ConfigContext.bindConfigObject(dataSourceSetting, "db3.");
		return dataSourceSetting;
	}
	
	@Bean
	public DataSourceSetting oaUpdateDataSourceSetting() {
		DataSourceSetting dataSourceSetting = new DataSourceSetting();
		ConfigContext.bindConfigObject(dataSourceSetting, "db4.");
		return dataSourceSetting;
	}
	
	@Bean
	public DataSourceSetting o2oh5DataSourceSetting() {
		DataSourceSetting dataSourceSetting = new DataSourceSetting();
		ConfigContext.bindConfigObject(dataSourceSetting, "db5.");
		return dataSourceSetting;
	}
	
	@Bean
	public DataSourceSetting crmDataSourceSettingDb() {
		DataSourceSetting dataSourceSetting = new DataSourceSetting();
		ConfigContext.bindConfigObject(dataSourceSetting, "db6.");
		return dataSourceSetting;
	}

	@Bean
	public DataSourceSetting cfsDataSourceSettingDb() {
		DataSourceSetting dataSourceSetting = new DataSourceSetting();
		ConfigContext.bindConfigObject(dataSourceSetting, "db7.");
		return dataSourceSetting;
	}
	
	@Bean
	public DataSource o2oh5DataSource() {
		DataSourceSetting dataSourceSetting = o2oh5DataSourceSetting();
		ConnectionPoolDataSource connectionPoolDataSource = new ConnectionPoolDataSource();
		connectionPoolDataSource.setDriverClassName(dataSourceSetting.getDriver());
		connectionPoolDataSource.setUrl(dataSourceSetting.getUrl());
		connectionPoolDataSource.setUsername(dataSourceSetting.getUsername());
		connectionPoolDataSource.setPassword(dataSourceSetting.getPassword());
		connectionPoolDataSource.setValidationQuery(dataSourceSetting.getValidationQuery());
		connectionPoolDataSource.setMaxActive(16);
		connectionPoolDataSource.setDatabaseName(dataSourceSetting.getDatabaseName());
		connectionPoolDataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
		return connectionPoolDataSource;
	}

	@Bean
	public DataSource o2oDataSource() {
		DataSourceSetting dataSourceSetting = o2oDataSourceSetting();
		ConnectionPoolDataSource connectionPoolDataSource = new ConnectionPoolDataSource();
		connectionPoolDataSource.setDriverClassName(dataSourceSetting.getDriver());
		connectionPoolDataSource.setUrl(dataSourceSetting.getUrl());
		connectionPoolDataSource.setUsername(dataSourceSetting.getUsername());
		connectionPoolDataSource.setPassword(dataSourceSetting.getPassword());
		connectionPoolDataSource.setValidationQuery(dataSourceSetting.getValidationQuery());
		connectionPoolDataSource.setMaxActive(16);
		connectionPoolDataSource.setDatabaseName(dataSourceSetting.getDatabaseName());
		connectionPoolDataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
		return connectionPoolDataSource;
	}

	@Bean
	public DataSource oaDataSource() {
		DataSourceSetting dataSourceSetting = oaDataSourceSetting();
		ConnectionPoolDataSource connectionPoolDataSource = new ConnectionPoolDataSource();
		connectionPoolDataSource.setDriverClassName(dataSourceSetting.getDriver());
		connectionPoolDataSource.setUrl(dataSourceSetting.getUrl());
		connectionPoolDataSource.setUsername(dataSourceSetting.getUsername());
		connectionPoolDataSource.setPassword(dataSourceSetting.getPassword());
		connectionPoolDataSource.setValidationQuery(dataSourceSetting.getValidationQuery());
		connectionPoolDataSource.setMaxActive(16);
		connectionPoolDataSource.setDatabaseName(dataSourceSetting.getDatabaseName());
		connectionPoolDataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
		return connectionPoolDataSource;
	}
	
	@Bean
	public DataSource reportDataSource() {
		DataSourceSetting dataSourceSetting = reportDataSourceSetting();
		ConnectionPoolDataSource connectionPoolDataSource = new ConnectionPoolDataSource();
		connectionPoolDataSource.setDriverClassName(dataSourceSetting.getDriver());
		connectionPoolDataSource.setUrl(dataSourceSetting.getUrl());
		connectionPoolDataSource.setUsername(dataSourceSetting.getUsername());
		connectionPoolDataSource.setPassword(dataSourceSetting.getPassword());
		connectionPoolDataSource.setValidationQuery(dataSourceSetting.getValidationQuery());
		connectionPoolDataSource.setMaxActive(16);
		connectionPoolDataSource.setDatabaseName(dataSourceSetting.getDatabaseName());
		connectionPoolDataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
		return connectionPoolDataSource;
	}
	@Bean
	public DataSource oaUpdateDataSource() {
		DataSourceSetting dataSourceSetting = oaUpdateDataSourceSetting();
		ConnectionPoolDataSource connectionPoolDataSource = new ConnectionPoolDataSource();
		connectionPoolDataSource.setDriverClassName(dataSourceSetting.getDriver());
		connectionPoolDataSource.setUrl(dataSourceSetting.getUrl());
		connectionPoolDataSource.setUsername(dataSourceSetting.getUsername());
		connectionPoolDataSource.setPassword(dataSourceSetting.getPassword());
		connectionPoolDataSource.setValidationQuery(dataSourceSetting.getValidationQuery());
		connectionPoolDataSource.setMaxActive(16);
		connectionPoolDataSource.setDatabaseName(dataSourceSetting.getDatabaseName());
		connectionPoolDataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
		return connectionPoolDataSource;
	}
	
	@Bean
	public DataSource crmDataSourceDb() {
		DataSourceSetting dataSourceSetting = crmDataSourceSettingDb();
		ConnectionPoolDataSource connectionPoolDataSource = new ConnectionPoolDataSource();
		connectionPoolDataSource.setDriverClassName(dataSourceSetting.getDriver());
		connectionPoolDataSource.setUrl(dataSourceSetting.getUrl());
		connectionPoolDataSource.setUsername(dataSourceSetting.getUsername());
		connectionPoolDataSource.setPassword(dataSourceSetting.getPassword());
		connectionPoolDataSource.setValidationQuery(dataSourceSetting.getValidationQuery());
		connectionPoolDataSource.setMaxActive(16);
		connectionPoolDataSource.setDatabaseName(dataSourceSetting.getDatabaseName());
		connectionPoolDataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
		connectionPoolDataSource.setConnectionInitSqls(initSqls);
		return connectionPoolDataSource;
	}
	@Bean
	public DataSource cfsDataSourceDb() {
		DataSourceSetting dataSourceSetting = cfsDataSourceSettingDb();
		ConnectionPoolDataSource connectionPoolDataSource = new ConnectionPoolDataSource();
		connectionPoolDataSource.setDriverClassName(dataSourceSetting.getDriver());
		connectionPoolDataSource.setUrl(dataSourceSetting.getUrl());
		connectionPoolDataSource.setUsername(dataSourceSetting.getUsername());
		connectionPoolDataSource.setPassword(dataSourceSetting.getPassword());
		connectionPoolDataSource.setValidationQuery(dataSourceSetting.getValidationQuery());
		connectionPoolDataSource.setMaxActive(16);
		connectionPoolDataSource.setDatabaseName(dataSourceSetting.getDatabaseName());
		connectionPoolDataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
		connectionPoolDataSource.setConnectionInitSqls(initSqls);
		return connectionPoolDataSource;
	}
	
	@Bean(name = "o2oh5TransactionManager")
	public PlatformTransactionManager o2oh5TransactionManager() {
		DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
		transactionManager.setDataSource(o2oh5DataSource());
		return transactionManager;
	}
	

	@Bean(name = "o2oTransactionManager")
	public PlatformTransactionManager o2oTransactionManager() {
		DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
		transactionManager.setDataSource(o2oDataSource());
		return transactionManager;
	}

	@Bean(name = "oaTransactionManager")
	public PlatformTransactionManager oaTransactionManager() {
		DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
		transactionManager.setDataSource(oaDataSource());
		return transactionManager;
	}

	@Bean(name = "o2oJdbcSession")
	public JdbcSession o2oJdbcSession() {
		JdbcSession jdbcSession = new JdbcSession();
		jdbcSession.setDataSource(this.o2oDataSource());
		return jdbcSession;
	}
	
	@Bean(name = "o2oh5JdbcSession")
	public JdbcSession o2oh5JdbcSession() {
		JdbcSession jdbcSession = new JdbcSession();
		jdbcSession.setDataSource(this.o2oh5DataSource());
		return jdbcSession;
	}

	@Bean(name = "oaJdbcSession")
	public JdbcSession oaJdbcSession() {
		JdbcSession jdbcSession = new JdbcSession();
		jdbcSession.setDataSource(this.oaDataSource());
		return jdbcSession;
	}
	
	@Bean(name = "reportJdbcSession")
	public JdbcSession reportJdbcSession() {
		JdbcSession jdbcSession = new JdbcSession();
		jdbcSession.setDataSource(this.reportDataSource());
		return jdbcSession;
	}
	@Bean(name = "oaUpdateJdbcSession")
	public JdbcSession oaUpdateJdbcSession() {
		JdbcSession jdbcSession = new JdbcSession();
		jdbcSession.setDataSource(this.oaUpdateDataSource());
		return jdbcSession;
	}
	
	@Bean(name = "crmJdbcSession")
	public JdbcSession crmJdbcSessionDb() {
		JdbcSession jdbcSession = new JdbcSession();
		jdbcSession.setDataSource(this.crmDataSourceDb());
		return jdbcSession;
	}
	@Bean(name = "cfsJdbcSession")
	public JdbcSession cfsJdbcSessionDb() {
		JdbcSession jdbcSession = new JdbcSession();
		jdbcSession.setDataSource(this.cfsDataSourceDb());
		return jdbcSession;
	}
	/*
	 * @Bean public DubboConsumerInterceptor dubboConsumerInterceptor() { return
	 * new DubboConsumerInterceptor(); }
	 */
	@Bean
	public static PropertySourcesPlaceholderConfigurer placehodlerConfigurer() {
		return new PropertySourcesPlaceholderConfigurer();
	}
	
	@Bean
	public FastDfsClient fastDfsClient() {
		return new FastDfsClient();
	}
	
	@Bean
	public FtpConfig ftpConfig() {
		FtpConfig ftpConfig = new FtpConfig();
		ConfigContext.bindConfigObject(ftpConfig);
		return ftpConfig;

	}
	
	@Bean(name = "mongoSessionG")
	public MongoSession mongoSession() {
		MongoSetting mongoSetting = new MongoSetting();
		ConfigContext.bindConfigObject(mongoSetting);
		return new MongoSession(mongoSetting);
	}
}
